package com.wephone.Student.ui;

import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
//import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.wephone.Student.R;

import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.GroupResponse;
import com.wephone.Student.bean.data.GroupInfo;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.Util;

public class GroupInviteActivity extends BaseActivity {
	private GroupInfo groupData;
	private ProgressDialog progressDialog;
	private RelativeLayout inviteFriend, inviteWeixin, inviteQuan, inviteSina,
			inviteQQ, inviteQzone;
	private UMSocialService mController;
	private String shareTxt;

	// 获取详情
	private MyResponseHandler getHandler() {
		MyResponseHandler handler = new MyResponseHandler(
				GroupInviteActivity.this, GroupResponse.class) {
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if (progressDialog != null) {
					progressDialog.dismiss();
				}
				if (getDataSuccess) {
					finish();
					Toast.makeText(getApplicationContext(), "申请提交成功",
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0, result);

				if (progressDialog != null) {
					progressDialog.dismiss();
				}
				Toast.makeText(getApplicationContext(),
						getString(R.string.network_error_txt),
						Toast.LENGTH_SHORT).show();
			}
		};
		return handler;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_invite);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			groupData = (GroupInfo) bundle.getSerializable("groupData");
			init();
		} else {
			finish();
			Toast.makeText(getApplicationContext(), "数据异常", Toast.LENGTH_SHORT)
					.show();
		}
		
	}

	private void init() {
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		
		shareTxt = "我在山楂僧的"+groupData.getName()+"群里，这里有好多你想认识的小伙伴喔，你也来加入吧!";
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
		addQQPlatform();
		addWXPlatform();
		
		findViewById(R.id.btn_back).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});

		// btn_quit_group = (Button)findViewById(R.id.btn_quit_group);
		inviteFriend = (RelativeLayout) findViewById(R.id.inviteFriend);
		inviteQQ = (RelativeLayout) findViewById(R.id.inviteQQ);
		inviteQuan = (RelativeLayout) findViewById(R.id.inviteQuan);
		inviteQzone = (RelativeLayout) findViewById(R.id.inviteQzone);
		inviteSina = (RelativeLayout) findViewById(R.id.inviteSina);
		inviteWeixin = (RelativeLayout) findViewById(R.id.inviteWeixin);
		
		mController.setShareContent(shareTxt);
	
		mController.setShareMedia(new UMImage(getApplicationContext(), R.drawable.ic_launcher));
		
	
		inviteFriend.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),InviteFriendActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("groupData", groupData);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		inviteQQ.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mController.directShare(getApplicationContext(), SHARE_MEDIA.QQ,new SnsPostListener() {
		            @Override
		            public void onStart() {
		                Toast.makeText(getApplicationContext(), "分享开始",Toast.LENGTH_SHORT).show();
		            }

		            @Override
		            public void onComplete(SHARE_MEDIA platform,int eCode, SocializeEntity entity) {
		                if(eCode == StatusCode.ST_CODE_SUCCESSED){
		                    Toast.makeText(getApplicationContext(), "分享成功",Toast.LENGTH_SHORT).show();
		                }else{
		                    Toast.makeText(getApplicationContext(), "分享失败",Toast.LENGTH_SHORT).show();
		                }
		            }
		    });
			}
		});
		inviteQuan.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				WeiXinShareContent weixinContent = new WeiXinShareContent();
				//设置分享文字
				weixinContent.setShareContent(shareTxt);
				//设置title
				weixinContent.setTitle("来自山楂僧的分享");
				//设置分享内容跳转URL
				weixinContent.setTargetUrl("http://www.imlxs.com");
				//设置分享图片
				weixinContent.setShareImage(new UMImage(getApplicationContext(), R.drawable.ic_launcher));
				
				mController.setShareMedia(weixinContent);
				
				mController.directShare(getApplicationContext(), SHARE_MEDIA.WEIXIN_CIRCLE,new SnsPostListener() {
		            @Override
		            public void onStart() {
		                Toast.makeText(getApplicationContext(), "分享开始",Toast.LENGTH_SHORT).show();
		            }

		            @Override
		            public void onComplete(SHARE_MEDIA platform,int eCode, SocializeEntity entity) {
		                if(eCode == StatusCode.ST_CODE_SUCCESSED){
		                    Toast.makeText(getApplicationContext(), "分享成功",Toast.LENGTH_SHORT).show();
		                }else{
		                    Toast.makeText(getApplicationContext(), "分享失败",Toast.LENGTH_SHORT).show();
		                }
		            }
		    });
			}
		});
		inviteQzone.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mController.directShare(getApplicationContext(), SHARE_MEDIA.QZONE,new SnsPostListener() {
		            @Override
		            public void onStart() {
		                Toast.makeText(getApplicationContext(), "分享开始",Toast.LENGTH_SHORT).show();
		            }

		            @Override
		            public void onComplete(SHARE_MEDIA platform,int eCode, SocializeEntity entity) {
		                if(eCode == StatusCode.ST_CODE_SUCCESSED){
		                    Toast.makeText(getApplicationContext(), "分享成功",Toast.LENGTH_SHORT).show();
		                }else{
		                    Toast.makeText(getApplicationContext(), "分享失败",Toast.LENGTH_SHORT).show();
		                }
		            }
		    });
			}
		});
		inviteSina.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mController.directShare(GroupInviteActivity.this, SHARE_MEDIA.SINA,new SnsPostListener() {
		            @Override
		            public void onStart() {
		                Toast.makeText(getApplicationContext(), "分享开始",Toast.LENGTH_SHORT).show();
		            }

		            @Override
		            public void onComplete(SHARE_MEDIA platform,int eCode, SocializeEntity entity) {
		                if(eCode == StatusCode.ST_CODE_SUCCESSED){
		                    Toast.makeText(getApplicationContext(), "分享成功",Toast.LENGTH_SHORT).show();
		                }else{
		                    Toast.makeText(getApplicationContext(), "分享失败",Toast.LENGTH_SHORT).show();
		                }
		            }
		    });
			}
		});
		inviteWeixin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mController.directShare(getApplicationContext(), SHARE_MEDIA.WEIXIN,new SnsPostListener() {
		            @Override
		            public void onStart() {
		                Toast.makeText(getApplicationContext(), "分享开始",Toast.LENGTH_SHORT).show();
		            }

		            @Override
		            public void onComplete(SHARE_MEDIA platform,int eCode, SocializeEntity entity) {
		                if(eCode == StatusCode.ST_CODE_SUCCESSED){
		                    Toast.makeText(getApplicationContext(), "分享成功",Toast.LENGTH_SHORT).show();
		                }else{
		                    Toast.makeText(getApplicationContext(), "分享失败",Toast.LENGTH_SHORT).show();
		                }
		            }
		    });
			}
		});
	}
	private void addQQPlatform() {
		String appId = "100530288";
		String appKey = "2042fd761ebf8495387ed78cc81b0b40";
		// 添加QQ支持, 并且设置QQ分享内容的target url
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(GroupInviteActivity.this,
				appId, appKey);
		qqSsoHandler.setTargetUrl("http://www.imlxs.com");
		qqSsoHandler.addToSocialSDK();
		
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(GroupInviteActivity.this, appId,
				appKey);
		qZoneSsoHandler.addToSocialSDK();
	}
	
	private void addWXPlatform() {
		// 注意：在微信授权的时候，必须传递appSecret
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appId = "wx035b9a7450e44f5e";
		String appSecret = "9d59ffbef758410ae191ec756210b75b";
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(GroupInviteActivity.this,
				appId,appSecret);
		wxHandler.addToSocialSDK();

		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(GroupInviteActivity.this,
				appId, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.setTitle(shareTxt);
		wxCircleHandler.setTargetUrl("http://www.imlxs.com");
		wxCircleHandler.addToSocialSDK();
	}
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    /**使用SSO授权必须添加如下代码 */
	    UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
	    if(ssoHandler != null){
	       ssoHandler.authorizeCallBack(requestCode, resultCode, data);
	    }
	}
}
